package com.tiger.admin.http.messageServer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiger.admin.Db;
import com.tiger.admin.SaveOperateLog;
import com.tiger.admin.dto.PaginationResult;
import com.tiger.admin.http.messageServer.dto.PageDto;
import com.tiger.db.entity.MessageServer;
import com.tiger.utils.ResponseUtil;
import com.tiger.utils.Utils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.List;

@RestController
@RequestMapping("/messageServer")
public class MessageServerController {
    private final Db db;

    public MessageServerController(Db db) {
        this.db = db;
    }

    @PostMapping("page")
    public Object page(@RequestBody PageDto page, @RequestAttribute("memberId") long memberId) {
        PaginationResult<MessageServer> result = new PaginationResult<>();

        LambdaQueryWrapper<MessageServer> wrapper = new LambdaQueryWrapper<>();
        if(page.getName() != null && !page.getName().isEmpty()) {
            wrapper.like(MessageServer::getName, page.getName());
        }


        long total = db.messageServer.count(wrapper);
        if(total > 0) {
            result.setTotal(total);

            wrapper.orderByDesc(MessageServer::getId);

            List<MessageServer> rows = db.messageServer.pagination(wrapper, page.getPageNo(), page.getPageSize());



            result.setRows(rows);
        }


        return ResponseUtil.successRes(result);
    }


    @SaveOperateLog("新增消息服务接口")
    @PostMapping("add")
    public Object add(@RequestBody @Validated MessageServer data, @RequestAttribute("memberId") long memberId) {

        Instant now = Instant.now();

        data.setCreateTime(now.getEpochSecond());


        boolean r = db.messageServer.save(data);

        return r ? ResponseUtil.successRes(data) : ResponseUtil.failRes();

    }

    @SaveOperateLog("修改消息服务接口")
    @PostMapping("update/{id}")
    public Object add(@RequestBody @Validated MessageServer data, @PathVariable Integer id, @RequestAttribute("memberId") long memberId) {
        MessageServer row = db.messageServer.getById(id);
        if(row == null) return ResponseUtil.successRes();


        Utils.copyPropertiesIgnoreNull(data, row);


        row.setId(id);


        boolean r = db.messageServer.updateById(row);

        if(r) {
            return ResponseUtil.successRes(row);
        }

        return ResponseUtil.failRes();

    }

    @SaveOperateLog("删除消息服务接口")
    @PostMapping("delete/{id}")
    public Object del(@PathVariable long id, @RequestAttribute("memberId") long memberId) {
        MessageServer row = db.messageServer.getOne(new LambdaQueryWrapper<MessageServer>().eq(MessageServer::getId, id));
        if(row == null) return ResponseUtil.successRes();
        boolean r = db.messageServer.removeById(id);
        if(r) {
            return ResponseUtil.successRes();
        }
        return ResponseUtil.failRes();
    }
}
